﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Config
{
    public class ChannelRotationList
    {
        XmlNode _xmlNode = null;
        XmlDocument _xmlDoc = null;
        List<Channel_rotation> _list = new List<Channel_rotation>();
        public ChannelRotationList(XmlNode Node)
        {
            if (Node == null)
            {
                throw new XmlException("Node is equal null");
            }
            _xmlNode = Node;
            _xmlDoc = _xmlNode.OwnerDocument;
        }

        public List<Channel_rotation> Channels
        {
            get
            {
                // List<BaseNode> lst = new List<BaseNode>();
                Channel_rotation c = null;

                if (null != _list)
                    _list.Clear();
                else
                    _list = new List<Channel_rotation>();

                foreach (XmlNode x in _xmlNode.SelectNodes("channel"))
                {
                    c = new Channel_rotation(x);
                    //  BaseNode b = c as BaseNode;
                    _list.Add(c);
                }
                //lst.Sort(delegate(BaseNode x, BaseNode y) { return x.id.CompareTo(y.id); });
                //foreach (BaseNode b in lst)
                //{
                //    _list.Add(b as Cls_Channel);
                //}
                _list.Sort();
                return _list;
            }
        }
        public class Channel_rotation:BaseNode
        {
            public Channel_rotation(XmlNode Node):base(Node)
            {
                if (Node == null)
                {
                    throw new XmlException("Node is equal null");
                }
            }
            public void Initial()
            {
                _xmlNode.Attributes.Append(_xmlDoc.CreateAttribute("NO")).Value = "";
                XmlNode node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("channelname"));
                node.InnerText = "";
                node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("enable"));
                node.InnerText = "";
            }
            //public int No
            //{
            //    get
            //    {
            //        if (_xmlNode.Attributes["NO"] == null)
            //        {
            //            return -1;
            //        }
            //        return int.Parse(_xmlNode.Attributes["NO"].Value);
            //    }
            //    set
            //    {
            //        if (_xmlNode.Attributes["NO"] == null)
            //        {
            //            XmlDocument doc = _xmlNode.OwnerDocument;
            //            _xmlNode.Attributes.Append(doc.CreateAttribute("NO")).Value = value.ToString();
            //        }
            //        else
            //            _xmlNode.Attributes["NO"].Value = value.ToString();
            //    }
            //}
            public string ChannelName
            {
                get
                {
                    if (_xmlNode == null)
                    {
                        return "";
                    }
                    return _xmlNode["channelname"].InnerText;
                }
                set
                {
                    XmlNode Node = _xmlNode.SelectSingleNode("channelname");
                    if (Node == null)
                        Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("channelname"));
                    Node.InnerText = value.ToString();
                }
            }
            public int Enable
            {
                get
                {
                    if (_xmlNode == null)
                    {
                        return -1;
                    }
                    return int.Parse(_xmlNode["enable"].InnerText);
                }
                set
                {
                    XmlNode Node = _xmlNode.SelectSingleNode("enable");
                    if (Node == null)
                        Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("enable"));
                    Node.InnerText = value.ToString();
                }
            }
            //<channelname>rotation1</channelname>
            //<enable>1</enable>
            //<rationdir>0</rationdir>
            public int rationdir
            {
                get
                {
                    if (_xmlNode == null)
                    {
                        return -1;
                    }
                    return int.Parse(_xmlNode["rationdir"].InnerText);
                }
                set
                {
                    XmlNode Node = _xmlNode.SelectSingleNode("rationdir");
                    if (Node == null)
                        Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("rationdir"));
                    Node.InnerText = value.ToString();
                }
            }
            //<JianXiangCao>0</JianXiangCao>
            public float JianXiangCao
            {
                get
                {
                    if (_xmlNode == null)
                    {
                        return -1.0f;
                    }
                    return float.Parse(_xmlNode["JianXiangCao"].InnerText);
                }
                set
                {
                    XmlNode Node = _xmlNode.SelectSingleNode("JianXiangCao");
                    if (Node == null)
                        Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("JianXiangCao"));
                    Node.InnerText = value.ToString();
                }
            }
            //<JianXiangCaoAuto>1</JianXiangCaoAuto>
            public int JianXiangCaoAuto
            {
                get
                {
                    if (_xmlNode == null)
                    {
                        return -1;
                    }
                    return int.Parse(_xmlNode["JianXiangCaoAuto"].InnerText);
                }
                set
                {
                    XmlNode Node = _xmlNode.SelectSingleNode("JianXiangCaoAuto");
                    if (Node == null)
                        Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("JianXiangCaoAuto"));
                    Node.InnerText = value.ToString();
                }
            }
            //<JianXiangCaoVolCompare>2</JianXiangCaoVolCompare>
            public float JianXiangCaoVolCompare
            {
                get
                {
                    if (_xmlNode == null)
                    {
                        return -1.0f;
                    }
                    return float.Parse(_xmlNode["JianXiangCaoVolCompare"].InnerText);
                }
                set
                {
                    XmlNode Node = _xmlNode.SelectSingleNode("JianXiangCaoVolCompare");
                    if (Node == null)
                        Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("JianXiangCaoVolCompare"));
                    Node.InnerText = value.ToString();
                }
            }
            //<ChiLunPan>0</ChiLunPan>
            public int ChiLunPan
            {
                get
                {
                    if (_xmlNode == null)
                    {
                        return -1;
                    }
                    return int.Parse(_xmlNode["ChiLunPan"].InnerText);
                }
                set
                {
                    XmlNode Node = _xmlNode.SelectSingleNode("ChiLunPan");
                    if (Node == null)
                        Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("ChiLunPan"));
                    Node.InnerText = value.ToString();
                }
            }
            //<ChiLunPanAuto>0</ChiLunPanAuto>
            public float ChiLunPanAuto
            {
                get
                {
                    if (_xmlNode == null)
                    {
                        return -1.0f;
                    }
                    return float.Parse(_xmlNode["ChiLunPanAuto"].InnerText);
                }
                set
                {
                    XmlNode Node = _xmlNode.SelectSingleNode("ChiLunPanAuto");
                    if (Node == null)
                        Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("ChiLunPanAuto"));
                    Node.InnerText = value.ToString();
                }
            }
            //<ChiLunPanCompareVol>0</ChiLunPanCompareVol>
            public float ChiLunPanCompareVol
            {
                get
                {
                    if (_xmlNode == null)
                    {
                        return -1.0f;
                    }
                    return float.Parse(_xmlNode["ChiLunPanCompareVol"].InnerText);
                }
                set
                {
                    XmlNode Node = _xmlNode.SelectSingleNode("ChiLunPanCompareVol");
                    if (Node == null)
                        Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("ChiLunPanCompareVol"));
                    Node.InnerText = value.ToString();
                }
            }
            //<ChiLunPanNum>7602273</ChiLunPanNum>
            public int ChiLunPanNum
            {
                get
                {
                    if (_xmlNode == null)
                    {
                        return -1;
                    }
                    return int.Parse(_xmlNode["ChiLunPanNum"].InnerText);
                }
                set
                {
                    XmlNode Node = _xmlNode.SelectSingleNode("ChiLunPanNum");
                    if (Node == null)
                        Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("ChiLunPanNum"));
                    Node.InnerText = value.ToString();
                }
            }
            //<ChiLunPanJianXiangCompareVol>3</ChiLunPanJianXiangCompareVol>
            public float ChiLunPanJianXiangCompareVol
            {
                get
                {
                    if (_xmlNode == null)
                    {
                        return -1.0f;
                    }
                    return float.Parse(_xmlNode["ChiLunPanJianXiangCompareVol"].InnerText);
                }
                set
                {
                    XmlNode Node = _xmlNode.SelectSingleNode("ChiLunPanJianXiangCompareVol");
                    if (Node == null)
                        Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("ChiLunPanJianXiangCompareVol"));
                    Node.InnerText = value.ToString();
                }
            }
            //<JianXiangCaoMax>10</JianXiangCaoMax>
            public float JianXiangCaoMax
            {
                get
                {
                    if (_xmlNode == null)
                    {
                        return -1.0f;
                    }
                    return float.Parse(_xmlNode["JianXiangCaoMax"].InnerText);
                }
                set
                {
                    XmlNode Node = _xmlNode.SelectSingleNode("JianXiangCaoMax");
                    if (Node == null)
                        Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("JianXiangCaoMax"));
                    Node.InnerText = value.ToString();
                }
            }
            //<JianXiangCaoMin>2</JianXiangCaoMin>
            public float JianXiangCaoMin
            {
                get
                {
                    if (_xmlNode == null)
                    {
                        return -1.0f;
                    }
                    return float.Parse(_xmlNode["JianXiangCaoMin"].InnerText);
                }
                set
                {
                    XmlNode Node = _xmlNode.SelectSingleNode("JianXiangCaoMin");
                    if (Node == null)
                        Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("JianXiangCaoMin"));
                    Node.InnerText = value.ToString();
                }
            }
            //<GearMax>1000</GearMax>
            public float GearMax
            {
                get
                {
                    if (_xmlNode == null)
                    {
                        return -1.0f;
                    }
                    return float.Parse(_xmlNode["GearMax"].InnerText);
                }
                set
                {
                    XmlNode Node = _xmlNode.SelectSingleNode("GearMax");
                    if (Node == null)
                        Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("GearMax"));
                    Node.InnerText = value.ToString();
                }
            }
            //<GearMin>20</GearMin>
            public float GearMin
            {
                get
                {
                    if (_xmlNode == null)
                    {
                        return -1.0f;
                    }
                    return float.Parse(_xmlNode["GearMin"].InnerText);
                }
                set
                {
                    XmlNode Node = _xmlNode.SelectSingleNode("GearMin");
                    if (Node == null)
                        Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("GearMin"));
                    Node.InnerText = value.ToString();
                }
            }
            //<GearJianxiang>10</GearJianxiang>
            public float GearJianxiang
            {
                get
                {
                    if (_xmlNode == null)
                    {
                        return -1.0f;
                    }
                    return float.Parse(_xmlNode["GearJianxiang"].InnerText);
                }
                set
                {
                    XmlNode Node = _xmlNode.SelectSingleNode("GearJianxiang");
                    if (Node == null)
                        Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("GearJianxiang"));
                    Node.InnerText = value.ToString();
                }
            }
            public float VirtualSpeed
            {
                get
                {
                    try
                    {
                        if (_xmlNode == null)
                        {
                            return -1.0f;
                        }
                        float virtualrate = float.Parse(_xmlNode["visualRate"].InnerText);
                        return virtualrate;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        return 0f;
                    }
                }
                set
                {
                    XmlNode Node = _xmlNode.SelectSingleNode("visualRate");
                    if (Node == null)
                        Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("visualRate"));
                    Node.InnerText = value.ToString();
                }
            }
        }
    }
}
